package com.xxx.video.dynamic.activitys;

import android.os.Bundle;

import androidx.recyclerview.widget.ConcatAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xxx.video.R;
import com.xxx.video.commen.activitys.BaseActivity;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.utils.StatusBarUtil;
import com.xxx.video.dynamic.adapters.DynamicListAdapter;
import com.xxx.video.dynamic.adapters.PersonInfoUserInfoAdapter;
import com.xxx.video.dynamic.beans.PersonInfoBean;
import com.xxx.video.dynamic.presenters.IPersonInfoView;
import com.xxx.video.dynamic.presenters.PersonInfoPresenter;
import com.xxx.video.mine.beans.DynamicListBean;

import butterknife.BindView;

public class PersonInfoActivity extends BaseActivity<PersonInfoPresenter> implements
        IPersonInfoView, BaseRecycleViewAdapter.OnItemClickListener<DynamicListBean.PageBean.ListBean> {


    @BindView(R.id.rcv_person_info_list)
    RecyclerView rcvPersonInfoList;
    /**
     * user id
     */
    private String mUserId;
    /**
     * user name
     */
    private String userName;
    /**
     * user avatar
     */
    private String userAvatar;
    private PersonInfoUserInfoAdapter mPersonInfoUserInfoAdapter;

    @Override
    protected PersonInfoPresenter createPresenter() {
        return new PersonInfoPresenter(this, this);
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_person_info;
    }

    @Override
    public void initToolbarData() {
        super.initToolbarData();
        showActionBar();
        StatusBarUtil.getInstance().setStatusBarTransparent(this, true);
        setIsShowActionbarLine(true);
        setActionBarTitle(R.string.title_person_info_activity);
    }

    @Override
    public void initData(Bundle extras) {
        if (extras != null) {
            mUserId = extras.getString("Id");
        }
        getPresenter().getFriendsInfo(mUserId);
    }

    @Override
    public void showRootLayoutStatus(int status) {
        showLayoutStatus(status);
    }

    @Override
    public void initData2View(PersonInfoBean successOb) {
        showRootLayoutStatus(1);
        rcvPersonInfoList.setLayoutManager(new LinearLayoutManager(this));
        ConcatAdapter concatAdapter = new ConcatAdapter();
        rcvPersonInfoList.setAdapter(concatAdapter);

        /*
         * user Name
         */
        userName = successOb.user.username;
        /*
         * avatar
         */
        userAvatar = successOb.user.picUrl;

        /*
         * person info
         */
        mPersonInfoUserInfoAdapter = new PersonInfoUserInfoAdapter(this);
        mPersonInfoUserInfoAdapter.setOnItemClickListener((o, position, type) -> {
            if (type == 0) {
                /*
                 * start add friend activity
                 */
                getPresenter().addFriends(successOb.user);
            } else if (type == 2) {
                /*
                 * start chat activity
                 */
                startChatActivity(mUserId);
            }
        });
        mPersonInfoUserInfoAdapter.setPersonData(successOb);
        concatAdapter.addAdapter(mPersonInfoUserInfoAdapter);
        /*
         * dynamic list
         */
        DynamicListAdapter mDynamicListAdapter = new DynamicListAdapter(this);
        concatAdapter.addAdapter(mDynamicListAdapter);
        mDynamicListAdapter.setOnItemClickListener(this);
        mDynamicListAdapter.setData(successOb.page.list);

    }

    @Override
    public void onItemClick(DynamicListBean.PageBean.ListBean listBean, int position, int type) {
        startVideoDetailsActivity(listBean.fId);
    }

    @Override
    public void startVideoDetailsActivity(String videoId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", videoId);
        intent2Activity(DynamicDetailsActivity.class, bundle);
    }

    @Override
    public void startChatActivity(String userId) {
        Bundle bundle = new Bundle();
        bundle.putString("Id", userId);
        bundle.putString("userName", userName);
        bundle.putString("userAvatar", userAvatar);
        intent2Activity(ChatActivity.class, bundle);
    }

    @Override
    public void addFriendSuccess() {
        if (mPersonInfoUserInfoAdapter != null) {
            mPersonInfoUserInfoAdapter.addFriendSuccess();
        }
    }

}
